package com.ruoyi.common.utils;

public class SmsCodeUtil {


    public static void main(String[] args){
        System.out.println(getCode("JM-RBLBNK\n" +
                "832904 OTP for IMPS txn of INR 1.00 to SOURCE ONE KOTAK on NetBanking & valid for 100 sec. If not initiated by you, call RBL Bank on 022-61156300\n" +
                "\n" +
                "2023-12-21 19:54:42\n" +
                "NS's A70s"));
        System.out.println(getCode("AX-ICICIO\n" +
                "Dear Customer, 007970 is the OTP to complete fund transfer of Rs 202750.00. OTPs are SECRET. DO NOT disclose it to anyone. ICICI Bank NEVER asks for OTP.\n" +
                "\n" +
                "2023-12-21 20:04:11\n" +
                "NS's A70s"));
        System.out.println(getCode("VM-KOTAKB\n" +
                "Do not share! 229692 is your OTP for CRN XXXXXX344 to log into Kotak Net Banking. To report fraud, click kotak.com/fraud - Kotak Bank\n" +
                "\n" +
                "2023-12-21 22:54:21\n" +
                "NS's A70s"));
    }

    public static  String getCode(String content){
        if (StringUtils.isEmpty(content)) return "";
        if (!content.contains("OTP")){
            return "";
        }
        String rst = content;
        String[] ss = content.split("\\n");
        if (ss.length>1){
            rst = ss[1];
        }

        int start = rst.indexOf("Dear Customer,");
        if (start>=0){
            rst = rst.substring(start+14,start+21);
        }else{
            start = rst.indexOf(" is the OTP for");
            if (start>=0){
                rst = rst.substring(start-6,start);
            }else{
                start = rst.indexOf("+is+the+OTP+for");
                if (start>=0){
                    rst = rst.substring(start-6,start+1);
                }else{
                    if (rst.contains(" is your OTP for")){
                        start = rst.indexOf(" is your OTP for");
                        rst = rst.substring(start-6,start);
                        return rst;
                    }
                    if (rst.contains(" OTP for ")){
                        rst = rst.substring(0,6);
                        return rst;
                    }
                }
            }
        }
        rst = rst.trim();
        if (rst.length()>20){
            rst = "";
        }
        return rst;
    }
}
